From 127f659c88078d6997c2b797a4c2fe4b1e063d8a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 28 Aug 2014 12:28:23 -0700 Subject: [PATCH] Print longer paths to Cargo.toml on failure Whenever possible, try to print a short path by using path_relative_from. Closes #404 --- src/cargo/util/toml.rs | 20 ++++++++++++++------ tests/test_cargo_compile.rs | 19 ++++++++++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 117b68d62..a268e6b70 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::fmt; use std::io::fs; +use std::os; use std::slice; use std::str; use toml; @@ -89,19 +90,26 @@ pub fn to_manifest(contents: &[u8], source_id: &SourceId, layout: Layout) -> CargoResult<(Manifest, Vec)> { + let manifest = layout.root.join("Cargo.toml"); + let manifest = match manifest.path_relative_from(&os::getcwd()) { + Some(path) => path, + None => manifest, + }; let contents = try!(str::from_utf8(contents).require(|| { - human("Cargo.toml is not valid UTF-8") + human(format!("{} is not valid UTF-8", manifest.display())) })); - let root = try!(parse(contents, &Path::new("Cargo.toml"))); + let root = try!(parse(contents, &manifest)); let mut d = toml::Decoder::new(toml::Table(root)); let toml_manifest: TomlManifest = match Decodable::decode(&mut d) { Ok(t) => t, - Err(e) => return Err(human(format!("Cargo.toml is not a valid \ - manifest\n\n{}", e))) + Err(e) => return Err(human(format!("{} is not a valid \ + manifest\n\n{}", + manifest.display(), e))) }; let pair = try!(toml_manifest.to_manifest(source_id, &layout).map_err(|err| { - human(format!("Cargo.toml is not a valid manifest\n\n{}", err)) + human(format!("{} is not a valid manifest\n\n{}", + manifest.display(), err)) })); let (mut manifest, paths) = pair; match d.toml { @@ -146,7 +154,7 @@ pub fn parse(toml: &str, file: &Path) -> CargoResult { let (loline, locol) = parser.to_linecol(error.lo); let (hiline, hicol) = parser.to_linecol(error.hi); error_str.push_str(format!("{}:{}:{}{} {}\n", - file.filename_display(), + file.display(), loline + 1, locol + 1, if loline != hiline || locol != hicol { format!("-{}:{}", hiline + 1, diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index fc4d1250a..eae7180f8 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -48,7 +48,6 @@ test!(cargo_compile_with_invalid_manifest { No `package` or `project` section found.\n")) }) - test!(cargo_compile_with_invalid_manifest2 { let p = project("foo") .file("Cargo.toml", r" @@ -63,6 +62,24 @@ test!(cargo_compile_with_invalid_manifest2 { Cargo.toml:3:19-3:20 expected a value\n\n")) }) +test!(cargo_compile_with_invalid_manifest3 { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/Cargo.toml", "a = bar"); + + assert_that(p.cargo_process("build").arg("--manifest-path") + .arg("src/Cargo.toml"), + execs() + .with_status(101) + .with_stderr("could not parse input TOML\n\ + src[..]Cargo.toml:1:5-1:6 expected a value\n\n")) +}) + test!(cargo_compile_with_invalid_version { let p = project("foo") .file("Cargo.toml", r#" -- 2.30.2